package com.nstar.carousel.base
{
	import com.nstar.carousel.components.ProductItem;
	import com.nstar.carousel.model.ProductVO;
	
	import flash.display.DisplayObject;
	
	import mx.core.ClassFactory;
	import mx.core.Container;
	import mx.core.IFactory;
	import mx.events.FlexEvent;

	/**
	 * 产品显示组件基类
	 * 2010.2
	 * 
	 */ 
	public class ProductsBaseClass extends Container
	{
		[Bindable]
		private var _selectedIndex:int ;
		[Bindable]
		private var _groupIndex:int = 0;
		[Bindable]
		private var _dataProvider:Array;
		private var _group:Array = new Array();
		//显示的幻灯片数量，奇数,目的是便于显示展示的幻灯片中的中间一张
		private var _slideCount:Number = 3;
		
		public var splited:Boolean = true;//是否将数据分组
		
		public var defaultWidth:Number = 330;//默认宽度
		public var defaultHeight:Number = 138;//默认高度
		public var selectedWidth:Number = 100;//选中项的宽度
		public var selectedHeight:Number = 80;//选中项的高度
		
		
		public var gap:Number = 10; //图片间的距离
		public var padding:Number = 10;//边距
		
		public static const TOP:String = "top";
		public static const MIDDLE:String = "middle";
		public static const BOTTOM:String = "bottom";
		public var verticalAlign:String = MIDDLE;//top,middle,bottom
		
		public var itemRenderer:IFactory = new ClassFactory(ProductItem);
		
		
		public function ProductsBaseClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
		}
		
		public function set selectedIndex(value:int):void{
			this._selectedIndex = value;
		}
		
		public function get selectedIndex():int{
			return this._selectedIndex;
		} 
		
		
		public function set groupIndex(value:int):void{
			this._groupIndex = value;
		}
		
		public function get groupIndex():int{
			return this._groupIndex;
		}		
		
		public function set group(value:Array):void{
			this._group = value;
		}
		
		public function get group():Array{
			return this._group;
		} 
		
		public function set slideCount(value:Number):void{
			this._slideCount;
		}
		
		public function get slideCount():Number{
			return this._slideCount;
		} 
		
		[Bindable]
		public function set dataProvider(value:Array):void{
			if(!value){
				throw new Error("数据设置错误，不可设置为空。");
			}
			this.removeAllChildren();
			
			this._dataProvider = value;
			
			//是否分组数据
			if(splited){
				//将数据分组，也可以直接使用dataProvider作为数据源
				group = split2group(_dataProvider,slideCount);
				//添加第一组产品项，作为默数据显示
				addProductItem(group[0]);
			}else{
				//如果不分组就直接将_dataProvider作为数据源
				addProductItem(_dataProvider);
			}

			//调用子类的layout方法对产品进行布局
			layout();
		}
		
		[Bindable]
		public function get dataProvider():Array{
			return this._dataProvider;
		}
		
		//添加产品项
		protected function addProductItem(items:Array):void{
			for each(var item:ProductVO in items){
				var pi:Object = itemRenderer.newInstance();
				pi.product = item;
				pi.width = this.defaultWidth;
				pi.height = this.defaultHeight;
				this.addChild(pi as DisplayObject);
			}
		}
		
		protected function layout():void{
			//在子类中实现你想要的布局方式，例如重新排列，添加动态效果等等
			throw new Error("请继承ProductsBaseClass后，在子类中实现layout()方法！");
		}
		
		
		//上一组
		public function priorGroup():void{
			if(!splited) return;
			
			groupIndex--;
			if(groupIndex<0){
				groupIndex = 0;
			}
			//重新布局，子类去实现
			layout();
		}		
		
		//下一组
		public function nextGroup():void{
			if(!splited) return;
			
			groupIndex++;
			if(groupIndex>group.length-1){
				groupIndex = group.length - 1;
			}
			//重新布局，子类去实现
			layout();
		}

		
		//取得中间的索引
		private function get middleIndex():Number{
			if(this.group){
				return Math.floor(this.group.length/2);
			}
			return 0;
		}		
		
		//将数据分组
		private function split2group(value:Array,count:int):Array{
			var group:Array = new Array();
			
			if(value){
				var m:Number = Math.floor(value.length/count);
				var n:Number = (value.length%count)>0?1:0;
				for(var i:int =0;i<(m+n);i++){
					var tmp:Array = value.slice(i*count,(i+1)*count);
					group.push(tmp);
				}
			}
			
			return group;
		}		
		
	}
}